package modules::cuffcompare;

sub new {
  my ($class,$config)    = @_;

  my $self = {
               _path         => $config->{'PATH'      },
               _job_name     => $config->{'JOB_NAME'  },
               _genome       => $config->{'GENOME'    },
               _gtf_file     => $config->{'GTF_FILE'  },
               _samples      => $config->{'SAMPLES'    },
             };
  bless $self,$class;
  return $self;
}

sub prepare {
  my ($self)                  = @_;
  my $job_name                = $self->{'_job_name'};
  my $path                    = $self->{'_path'    };
  my $samples                 = $self->{'_samples' };
  my $job_dir                 = $path . '/results/' . $job_name;
  my $cufflinks_out           = $job_dir . '/Cufflinks/'  ;
  my $cuffcompare_out         = "$job_dir/Cuffcompare";
  $self->{'_cufflinks_out'}   = $cufflinks_out;
  
  
  $self->{'_cuffcompare_out'} = $cuffcompare_out;
  
  print "Creating '$cuffcompare_out'...  \n$cuffcompare_out\n";
  mkdir  $cuffcompare_out, 777; chmod(0777, $cuffcompare_out);
  
  return;
}

sub run {
  my ($self)            = @_;
  my $cuffcompare_out   = $self->{'_cuffcompare_out' };
  my $genome            = $self->{'_genome'          };
  my $cufflinks_out     = $self->{'_cufflinks_out'   };
  my $gtf_file          = $self->{'_gtf_file'        };
  my $samples           = $self->{'_samples'        };
  my $i=1;
#use Data::Dumper;
#warn 'samples:'.Dumper $samples;

  my $cmd     = qq( bsub -J cuff_$i -n 4 -R "span[ptile=4] rusage[mem=5000]" )
             .  qq( /usr/users/ga002/corpasm/workarea/bin/cuffcompare )
             .  qq( -r $gtf_file -o $cuffcompare_out -p 8);
             #.  qq( -s $genome.fa -p 8 );
   
   map { $cmd .= qq( $cufflinks_out/$_->[0]/transcripts.gtf ) } @$samples;
  print "\n\nRUNNING COMMAND:\n$cmd\n\n";
  system($cmd);

  my $wait = 1;
  while ($wait == 1) {
    my  @info = qx(bjobs);
    print scalar localtime() . "\n";
    if(my @cufflinks_jobs = grep(/links_/, @info) ) { map{print $_}@cufflinks_jobs; sleep 10 }
    else {$wait = 0}  
  }  
  return;

}
1; 


